package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.BaseDutyRule;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.DutyRule;
import com.syxp.pda.service.base.DutyRuleService;

public class DutyRuleAction extends BaseAction{

	@Resource
	private DutyRuleService dutyRuleService ;

	public DutyRuleService getDutyRuleService() {
		return dutyRuleService;
	}

	public void setDutyRuleService(DutyRuleService dutyRuleService) {
		this.dutyRuleService = dutyRuleService;
	}
	
	private int departmentID ;
	
	private String baseDutyStr ;
	
	private String dutyRuleStr ;
	
	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	public String getBaseDutyStr() {
		return baseDutyStr;
	}

	public void setBaseDutyStr(String baseDutyStr) {
		this.baseDutyStr = baseDutyStr;
	}

	public String getDutyRuleStr() {
		return dutyRuleStr;
	}

	public void setDutyRuleStr(String dutyRuleStr) {
		this.dutyRuleStr = dutyRuleStr;
	}

	public String loadDutyRule() {
		try{
			Department department = dutyRuleService.loadDutyRuleInfo(departmentID) ;
			JsonObject Jobj = new JsonObject() ;
			if(department.getBaseDutyRule()==null){
				
				Jobj.add( "classes",gson.toJsonTree(department.getClassDutys())) ;
				JsonArray jsonDutys = new JsonArray() ;
				for(Duty duty : department.getDutys()) {
					JsonObject jduty = new JsonObject() ;
					jduty.addProperty("dutyID", duty.getDutyID()) ;
					jduty.addProperty("dutyName", duty.getDutyName());
					jsonDutys.add(jduty);
				}
				Jobj.add("duty", jsonDutys) ;
				Jobj.addProperty("success", true) ;
				System.out.println(gson.toJson(Jobj)) ;
			}else{
				Jobj.add( "classes",gson.toJsonTree(department.getClassDutys())) ;
				JsonObject baseJson = new JsonObject() ;
				baseJson.addProperty("cricleDays", department.getBaseDutyRule().getCircleDays()) ;
				baseJson.addProperty("span", department.getBaseDutyRule().getSpan()) ;
				baseJson.addProperty("baseDate",
						TimeFormatUtil.formatDate(department.getBaseDutyRule().getBaseDate())) ;
				Jobj.add("baseDutyRule", baseJson) ;
				
				int tempInt = 0 ;
				JsonArray jsonDutyRules = new JsonArray() ;
				JsonObject jsonDutyRule = null ;
				for(int i=1 ; i<=department.getDutyRules().size() ; i++) {
					DutyRule dutyRule = department.getDutyRules().get(i-1) ;
					if(dutyRule.getDuRuOrder()!=tempInt) {
						tempInt = dutyRule.getDuRuOrder() ;
						jsonDutyRule = new JsonObject() ;
						jsonDutyRule.addProperty("duRuOrder", tempInt) ;
					}
					jsonDutyRule.addProperty(""+dutyRule.getClassID(), dutyRule.getDutyID()) ;
					if(i%department.getClassDutys().size()==0) {
						jsonDutyRules.add(jsonDutyRule) ;
					}
				}
				Jobj.add("dutyRules",jsonDutyRules) ;
				JsonArray jsonDutys = new JsonArray() ;
				for(Duty duty : department.getDutys()) {
					JsonObject jduty = new JsonObject() ;
					jduty.addProperty("dutyID", duty.getDutyID()) ;
					jduty.addProperty("dutyName", duty.getDutyName());
					jsonDutys.add(jduty);
				}
				Jobj.add("duty", jsonDutys) ;
				System.out.println(gson.toJson(Jobj)) ;
			}
			Jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(Jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	
	public String saveDutyRule() {
		try{
			System.out.println(baseDutyStr) ;
			
			Gson gson = new GsonBuilder()  
			  .setDateFormat("yyyy-MM-dd HH:mm:ss")  
			  .create(); 
			BaseDutyRule baseDutyRule = gson.fromJson(baseDutyStr, BaseDutyRule.class) ;
			Department dept = new Department() ;
			dept.setDepartmentID(departmentID);
			baseDutyRule.setDepartment(dept) ;
			System.out.println(baseDutyRule.getCircleDays()) ; 
			List<DutyRule> all = gson.fromJson(dutyRuleStr, new TypeToken<List<DutyRule>>() {}.getType()) ;
			dutyRuleService.saveBaseDutyRule(departmentID, baseDutyRule, all) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
}
